package nz.jdbcwrapper;

import java.sql.SQLException;
import java.sql.Clob;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

class WrapperClob implements Clob
{
  final Clob clob;
  final Data data;
  final String prefix;
  final long id = Util.id();

  WrapperClob(Data data, String parentPrefix, Clob clob)
  {
    this.clob = clob;
    this.data = data;
    this.prefix = parentPrefix + "[" + id + "]";
    if (data.trace >= 5)
      Util.trace(prefix + " new Clob");
  }

  public long length() throws SQLException
  {
    long key = 0L;

    if (data.trace >= 5)
      key = Util.traceBegin(prefix + " length()");

    try
    {
      long result = clob.length();

      if (data.trace >= 5)
        Util.traceEnd(key, String.valueOf(result));
      return result;
    }
    catch (SQLException e)
    {
      if (data.trace >= 5)
        Util.traceEnd(key, e);
      throw e;
    }
  }

  public String getSubString(long pos, int length) throws SQLException
  {
    long key = 0L;

    if (data.trace >= 5)
      key = Util.traceBegin(prefix + " getSubString(" + pos + ", " + length + ")");

    try
    {
      String result = clob.getSubString(pos, length);

      if (data.trace >= 5)
        Util.traceEnd(key, String.valueOf(result));
      return result;
    }
    catch (SQLException e)
    {
      if (data.trace >= 5)
        Util.traceEnd(key, e);
      throw e;
    }
  }

  public Reader getCharacterStream() throws SQLException
  {
    long key = 0L;

    if (data.trace >= 5)
      key = Util.traceBegin(prefix + " getCharacterStream()");

    try
    {
      Reader result = clob.getCharacterStream();

      if (data.trace >= 5)
        Util.traceEnd(key, Util.toString(result));
      return result;
    }
    catch (SQLException e)
    {
      if (data.trace >= 5)
        Util.traceEnd(key, e);
      throw e;
    }
  }

  public InputStream getAsciiStream() throws SQLException
  {
    long key = 0L;

    if (data.trace >= 5)
      key = Util.traceBegin(prefix + " getAsciiStream()");

    try
    {
      InputStream result = clob.getAsciiStream();

      if (data.trace >= 5)
        Util.traceEnd(key, Util.toString(result));
      return result;
    }
    catch (SQLException e)
    {
      if (data.trace >= 5)
        Util.traceEnd(key, e);
      throw e;
    }
  }

  public long position(String searchstr, long start) throws SQLException
  {
    long key = 0L;

    if (data.trace >= 5)
      key = Util.traceBegin(prefix + " position(" + searchstr + ", " + start + ")");

    try
    {
      long result = clob.position(searchstr, start);

      if (data.trace >= 5)
        Util.traceEnd(key, String.valueOf(result));
      return result;
    }
    catch (SQLException e)
    {
      if (data.trace >= 5)
        Util.traceEnd(key, e);
      throw e;
    }
  }

  public long position(Clob searchstr, long start) throws SQLException
  {
    long key = 0L;

    if (data.trace >= 5)
      key = Util.traceBegin(prefix + " position(Clob, " + start + ")");

    try
    {
      long result = clob.position(searchstr, start);

      if (data.trace >= 5)
        Util.traceEnd(key, String.valueOf(result));
      return result;
    }
    catch (SQLException e)
    {
      if (data.trace >= 5)
        Util.traceEnd(key, e);
      throw e;
    }
  }
  /**
   * @see java.sql.Clob#setAsciiStream(long)
   */
  public OutputStream setAsciiStream(long pos) throws SQLException
  {
    long key = 0L;

    if (data.trace >= 5)
      key = Util.traceBegin(prefix + " setAsciiStream(" + pos + ")");

    try
    {
      OutputStream result = clob.setAsciiStream(pos);

      if (data.trace >= 5)
        Util.traceEnd(key, Util.toString(result));
      return result;
    }
    catch (SQLException e)
    {
      if (data.trace >= 5)
        Util.traceEnd(key, e);
      throw e;
    }
  }

  /**
   * @see java.sql.Clob#setCharacterStream(long)
   */
  public Writer setCharacterStream(long pos) throws SQLException
  {
    long key = 0L;

    if (data.trace >= 5)
      key = Util.traceBegin(prefix + " setCharacterStream(" + pos + ")");

    try
    {
      Writer result = clob.setCharacterStream(pos);

      if (data.trace >= 5)
        Util.traceEnd(key, Util.toString(result));
      return result;
    }
    catch (SQLException e)
    {
      if (data.trace >= 5)
        Util.traceEnd(key, e);
      throw e;
    }
  }

  /**
   * @see java.sql.Clob#setString(long, String, int, int)
   */
  public int setString(long pos, String str, int offset, int len)
    throws SQLException
  {
    long key = 0L;

    if (data.trace >= 5)
      key = Util.traceBegin(prefix + " setString(" + pos + ", " + str + ", " + offset + ", " + len + ")");

    try
    {
      int result = clob.setString(pos, str, offset, len);

      if (data.trace >= 5)
        Util.traceEnd(key, String.valueOf(result));
      return result;
    }
    catch (SQLException e)
    {
      if (data.trace >= 5)
        Util.traceEnd(key, e);
      throw e;
    }
  }

  /**
   * @see java.sql.Clob#setString(long, String)
   */
  public int setString(long pos, String str) throws SQLException
  {
    long key = 0L;

    if (data.trace >= 5)
      key = Util.traceBegin(prefix + " setString(" + pos + ", " + str + ")");

    try
    {
      int result = clob.setString(pos, str);

      if (data.trace >= 5)
        Util.traceEnd(key, String.valueOf(result));
      return result;
    }
    catch (SQLException e)
    {
      if (data.trace >= 5)
        Util.traceEnd(key, e);
      throw e;
    }
  }

  /**
   * @see java.sql.Clob#truncate(long)
   */
  public void truncate(long len) throws SQLException
  {
    long key = 0L;

    if (data.trace >= 5)
      key = Util.traceBegin(prefix + " truncate(" + len + ")");

    try
    {
      clob.truncate(len);

      if (data.trace >= 5)
        Util.traceEnd(key, (String)null);
    }
    catch (SQLException e)
    {
      if (data.trace >= 5)
        Util.traceEnd(key, e);
      throw e;
    }
  }

//    public void free() throws SQLException
//    {
//        clob.free();
//    }
//    
//    public Reader getCharacterStream(long pos, long length) throws SQLException
//    {
//        return clob.getCharacterStream(pos, length);
//    }

}
